import 'package:dio/dio.dart';
import 'package:mall/core/models/bank.dart';
import 'package:mall/core/services/api.dart';
import 'package:mall/core/providers/view_model.dart';

class BankListModel extends ViewModel {
  final Api api;
  final bool selectable;

  String _errorMessage;
  List<Bank> _list;

  BankListModel(this.api, this.selectable) : super() {
    _initData();
  }

  String get errorMessage => _errorMessage;

  List<Bank> get list => _list;

  Future<ViewState> _initData() async {
    viewState = ViewState.busy;
    try {
      _list = await api.bankList();
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }

  Future<ViewState> refreshData() async {
    try {
      _list = await api.bankList();
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
